/*
 * CppLocation.c
 *
 *  Created on: 2012/04/12
 *      Author: guanxiaoying
 */
#include "../coredef.h"
#include "cppdef.h"
typedef struct _CPPLOCATION{
	U2 usOffsetSize ;
	U2 usOffsetSize_Begin ;
	S4 line_begin ;
	S4 line_end ;
}CPPLOCATION;
static CPPLOCATION stCppLocation = { 0, 0, 0, 0 };

/******************************************************************
* 関 数 名	 : CppLineFeed
* 機    能	 : 改行識別子に会った時テーブルKIND_CURRENTINFOの処理
* 引    数 	 : なし
* 戻 り 値   : なし
* 詳細内容 : テーブルKIND_CURRENTINFOの最後の行のLONG_CURRENTNO属性に１を足す
* 　　　　　　　：  テーブルKIND_CURRENTINFOの最後の行のLONG_STARTNO属性に１を足す
*******************************************************************/
S4 CppLineFeed(VOID)
{
    AST    astTableRow;
	S4   lCurrentNo;

    /* テーブルKIND_CURRENTINFO連番計算 */
    astTableRow = stCppNestFileInfoAccess.getMax();
    if  ( astTableRow < MIN_ARRAY ){
    	stCppLocation.usOffsetSize = 0;
    	return ( COEXIT_SUCCESS );
    }
	/* テーブルKIND_CURRENTINFOの最後の行のLONG_CURRENTNO属性に１を足す */
    lCurrentNo = stCppNestFileInfoAccess.physicalLine.get( astTableRow );
	lCurrentNo ++;
	stCppNestFileInfoAccess.physicalLine.set( astTableRow, lCurrentNo );

	/* テーブルKIND_CURRENTINFOの最後の行のLONG_STARTNO属性に１を足す */
    lCurrentNo = stCppNestFileInfoAccess.logicLine.get( astTableRow );
	lCurrentNo ++;
	stCppNestFileInfoAccess.logicLine.set( astTableRow, lCurrentNo );
	stCppLocation.line_end++;
	stCppLocation.usOffsetSize = 1;
	return ( COEXIT_SUCCESS );
}
VOID CppLocScopeStart(VOID)
{
	stCppLocation.usOffsetSize_Begin = stCppLocation.usOffsetSize;
	stCppLocation.line_begin = stCppNestFileInfoAccess.physicalLine.get( stCppNestFileInfoAccess.getMax());
	if( stCppLocation.line_begin != 0 ){
		if( stCppLocation.usOffsetSize_Begin == 0 ){
			stCppLocation.usOffsetSize_Begin = 1;
			stCppLocation.usOffsetSize = 1;
		}
	}
	stCppLocation.line_end = stCppLocation.line_begin ;
}
VOID CppLocScopeStep( S4 steps )
{
	stCppLocation.usOffsetSize += steps;
}
S4 CppLocGetCodeScope(AST *pastCodeScp)
{
	CODESCOPE_CMP  codeScp_cmp;
	AST nKindIndex;

	/*pastLocはNULLである場合COEXIT_SUCCESS　を返す*/
	if( pastCodeScp == NULL )
	{
		return (COEXIT_SUCCESS);
	}
	*pastCodeScp = 0;
	nKindIndex = stCppNestFileInfoAccess.getMax();
	if( nKindIndex < MIN_ARRAY )
	{
		return COEXIT_SUCCESS;
	}
	codeScp_cmp.fileindex = stCppNestFileInfoAccess.fileIndex.get( nKindIndex );
	codeScp_cmp.beginLine = stCppLocation.line_begin;
	codeScp_cmp.endLine = stCppLocation.line_end;
	codeScp_cmp.offset_begin = stCppLocation.usOffsetSize_Begin;
	codeScp_cmp.offset_end = stCppLocation.usOffsetSize>0?stCppLocation.usOffsetSize-1:0;

	*pastCodeScp = stCoreCodeScpAccess.getCodeScp( &codeScp_cmp );
	if( *pastCodeScp == 0 )
	{
		return (COEXIT_FATAL);
	}
	return( COEXIT_SUCCESS );
}
S4 CppLocSetDirectiveScope( AST nNewDirInfo )
{
	AST nScope_begin = 0, nScope_end = 0;
	AST nKindMax;
	AST nCnt;
	U2 usTokenKind;
	/* 位置情報取得し、設定 */
	nKindMax = stCppTokenAccess.getMax();
	for( nCnt = MIN_ARRAY; nCnt <= nKindMax; nCnt++ )
	{
		usTokenKind = stCppTokenAccess.tokenKind.get( nCnt );
		if(  ( usTokenKind == xPPTOKEN_HSPACE )
		   ||( usTokenKind == xPPTOKEN_VSPACE )
		   ||( usTokenKind == xPPTOKEN_NEXTLINE ))
		{
			continue;
		}
		else
		{
			nScope_begin = stCppTokenAccess.codeScp.get( nCnt );
			break;
		}
	}
	if( nScope_begin >= MIN_ARRAY )
	{
		for( nCnt = nKindMax; nCnt >= MIN_ARRAY; nCnt-- )
		{
			usTokenKind = stCppTokenAccess.tokenKind.get( nCnt );
			if(  ( usTokenKind == xPPTOKEN_HSPACE )
			   ||( usTokenKind == xPPTOKEN_VSPACE )
			   ||( usTokenKind == xPPTOKEN_NEXTLINE ))
			{
				continue;
			}
			else
			{
				nScope_end = stCppTokenAccess.codeScp.get( nCnt );
				break;
			}
		}
	}
	if( nScope_begin != 0 && nScope_end != 0 )
	{
		nScope_begin = stCoreCodeScpAccess.mergeCodeScp( nScope_begin, nScope_end );
		if( nScope_begin == 0 )
		{
			return COEXIT_FATAL;
		}

		stCoreDirectiveInfoAccess.locScope.set(nNewDirInfo, nScope_begin);
	}
	return COEXIT_SUCCESS;
}
